package vip.george.order.service.impl;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.george.common.constant.ResultCodeEnum;
import vip.george.common.entity.ScOrder;
import vip.george.common.entity.ScUser;
import vip.george.common.exception.AppException;
import vip.george.order.dao.OrderDao;
import vip.george.order.dto.request.GeneraOrderRequest;
import vip.george.order.service.OrderService;
import vip.george.order.service.feign.FeignUserClient;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Objects;

/**
 * @author george
 * @since 2023/7/5
 * <p>订单的service</p>
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private FeignUserClient feignUserClient;


    @Override
    public Boolean generateOrder(GeneraOrderRequest request) {
        Timestamp nowTimestamp = new Timestamp(System.currentTimeMillis());
        // 根据买家id查询买家的信息
        ScUser user = feignUserClient.queryUser(request.getBuyerId()).getDataAndCheckSuccess();
        if (Objects.isNull(user)){
            throw new AppException(ResultCodeEnum.USER_NOT_EXIST);
        }

        ScOrder order = new ScOrder();
        order.setOrderSn("tb" + RandomUtil.randomNumbers(10));
        order.setBuyerId(request.getBuyerId());
        order.setPayAmount(new BigDecimal("100"));
        order.setProductId(request.getProductId());
        order.setState("001");
        order.setDeleteFlag(0);
        order.setCreateTime(nowTimestamp);
        order.setUpdateTime(nowTimestamp);
        orderDao.insertEntity(order);
        return true;
    }
}
